/*
 * @Description: 这个插件负责实例化 WebSocketService 并通过 provide 提供给整个应用。
 * @Author: redxing96@163.com
 * @Date: 2025-09-22 13:48:01
 * @LastEditTime: 2025-09-22 13:50:28
 * @LastEditors: front end cabbage
 * @FilePath: /dora-admin/src/plugins/websocket.ts
 */
import { type App, type InjectionKey } from "vue";
import { WebSocketService, type WebSocketOptions } from "@/services/websocketService";

// 创建一个 InjectionKey，用于 provide 和 inject，提供类型安全
export const WebSocketInjectKey: InjectionKey<WebSocketService> = Symbol("WebSocketService");

export default {
  install(app: App, options: WebSocketOptions) {
    if (!options || !options.url) {
      throw new Error("WebSocket plugin requires a URL in options.");
    }

    // 1. 创建 WebSocketService 的单例
    const wsService = new WebSocketService(options);

    // 2. 使用 provide 将实例提供给所有后代组件
    app.provide(WebSocketInjectKey, wsService);

    // 3. (可选) 也可以挂载到全局属性，方便在 Options API 中使用
    app.config.globalProperties.$ws = wsService;
  }
};
